package com.zhipu.resetbalance.ui.home.feature.compute;

import android.text.TextUtils;

import androidx.databinding.ObservableField;
import androidx.lifecycle.ViewModel;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.ConvertUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.zhipu.resetbalance.common.Constants;
import com.zhipu.resetbalance.listener.SerialPortListener;
import com.zhipu.resetbalance.mobile.dao.NumberedEntityDao;
import com.zhipu.resetbalance.mobile.event.NumberedEntity;
import com.zhipu.resetbalance.utils.CommonUtils;
import com.zhipu.resetbalance.utils.popup.PopupUtils;
import com.zhipu.resetbalance.utils.serialPort.WeightSerialPortUtils;
import com.zhipu.resetbalance.utils.weight.WeightStringUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

public class ComputeViewModel extends ViewModel  implements SerialPortListener {

    /**
     *
     */
    public ObservableField<String> WeighingWeight = new ObservableField<String>("0.00");


    /**
     * 编号
     */
    public ObservableField<String> mNumber = new ObservableField<String>("");


    /**
     * 日期
     */
    public ObservableField<String> mInspectionDate = new ObservableField("");

    /**
     * 类型
     */
    public ObservableField<Integer> breedNumber = new ObservableField<Integer>(0);

    /**
     * 定时器
     */
    private Disposable mDisposable;

    /**
     * 操作中
     */
    private boolean InOperation = false;


    /**
     * 字段一
     */
    public ObservableField<String> FieldOne = new ObservableField("");
    /**
     * 字段二
     */
    public ObservableField<String> FieldTwo = new ObservableField("");
    /**
     * 字段三
     */
    public ObservableField<String> FieldThree = new ObservableField("");
    /**
     * 字段四
     */
    public ObservableField<String> FieldFour = new ObservableField("");
    /**
     * 字段五
     */
    public ObservableField<String> Fieldfive = new ObservableField("");
    /**
     * 字段六
     */
    public ObservableField<String> FieldSix = new ObservableField("");
    /**
     * 字段七
     */
    public ObservableField<String> FieldSeven = new ObservableField("");


    /**
     * 初始化
     */
    public void initialize(){

    }



    /**
     *
     */
    public void milliseconds(){
        /**
         * 设置回调
         */
        WeightSerialPortUtils.setSerialPortListener(this);
        /**
         *
         */
        mDisposable = Observable.interval(1, TimeUnit.MILLISECONDS).subscribe(number ->{
            if(!InOperation){
                /**
                 * 重量輸出命令
                 */
                WeightSerialPortUtils.WeightOutputCommand();
            }
        });
    }

    /**
     * 释放定时器
     */
    public void ReleaseTimer(){
        if (mDisposable != null) {
            mDisposable.dispose();
            mDisposable = null;
        }
    }


    /**
     * 返回
     */
    public void FunctionalBalance(){
        //返回
        ActivityUtils.finishActivity(ComputeActivity.class);
    }


    /**
     * 点击一
     */
    public void clickOne(){
        FieldOne.set(WeighingWeight.get());
    }

    /**
     * 点击二
     */
    public void clickTwo(){
        FieldTwo.set(WeighingWeight.get());
    }
    /**
     * 点击三
     */
    public void clickThree(){
        FieldThree.set(WeighingWeight.get());
    }

    /**
     * 点击四
     */
    public void clickFour(){
        FieldFour.set(WeighingWeight.get());
    }





    /**
     * 打印
     */
    public void print(){

    }

    /**
     * 保存
     */
    public void save(){


    }


    /**
     * 计算
     */
    public void calculation(){
        /**
         * 编号
         */
        NumberedEntity mNumberedEntity = NumberedEntityDao.getInstance().acquireNumber();
        if(null != mNumberedEntity){
            mNumber.set(mNumberedEntity.NumberedName + TimeUtils.getNowMills());
        }else {
            mNumber.set(TimeUtils.getNowMills() + "");
        }

        /**
         * 日期
         */
        long getNowMills = TimeUtils.getNowMills();
        mInspectionDate.set(TimeUtils.millis2String(getNowMills));
        /**
         * 计算大样杂质率
         */
        double largeImpurityRate = CommonUtils.formatDouble(Double.parseDouble(FieldThree.get()) / Double.parseDouble(FieldOne.get()) * 100);
        //大样杂质率
        Fieldfive.set(largeImpurityRate + "");
        /**
         * 计算小样杂质率
         */
        double sampleImpurityRate = CommonUtils.formatDouble(Double.parseDouble(FieldFour.get()) / Double.parseDouble(FieldTwo.get()) * (100 - largeImpurityRate));
        //小样杂质率
        FieldSix.set(sampleImpurityRate + "");
        /**
         * 计算杂质总量
         */
        double totalImpurity = CommonUtils.formatDouble(largeImpurityRate + sampleImpurityRate);
        //总杂质量
        FieldSeven.set(totalImpurity + "");
    }


    /**
     * 去皮
     */
    public void peel(){
        //操作中
        InOperation = true;
        /**
         * 显示弹框
         */
        PopupUtils.showBeCommonPopup(ActivityUtils.getTopActivity());
        PopupUtils.SettingBeCommonPopup("正在去皮请稍后...");
        /**
         * 置零命令
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                WeightSerialPortUtils.ZeroOrder();
            }
        }).start();

    }




    @Override
    public void itemClicked(byte[] event) {
        //byte[]转字符串
        String numericalValue = ConvertUtils.bytes2HexString(event);
//        System.out.println(numericalValue);
        /**
         *
         */
        if(numericalValue.equals(Constants.Calibration100)){//標定100
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码100");
        }else if(numericalValue.equals(Constants.Calibration200)){//標定200
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码200");
        }else if(numericalValue.equals(Constants.Calibration500)){//標定500
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码500");
        }else if(numericalValue.equals(Constants.Calibration1000)){//標定1000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码1000");
        }else if(numericalValue.equals(Constants.Calibration2000)){//標定2000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码2000");
        }else if(numericalValue.equals(Constants.Calibration3000)){//標定3000
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("砝码3000");
        }else if(numericalValue.equals(Constants.SEND_GO_START)){//去皮完成
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("去皮成功");
            //操作中
            InOperation = false;
        }else if(numericalValue.equals(Constants.overweight)){//超重
            WeighingWeight.set("超重");
        }else if(numericalValue.equals(Constants.SHIELD)){//校准中
            //关闭弹框
            PopupUtils.dismissAwaitTipsPopup();
            //
            WeighingWeight.set("校准中");
        }else if(numericalValue.equals(Constants.CalibrationComplete)){//校准完成
            WeighingWeight.set("校准完成");
            //操作中
            InOperation = false;
        }else {
            String weightHandle = WeightStringUtils.weightHandle(event);
            /**
             * 打印重量
             */
            WeighingWeight.set(weightHandle);
        }
    }
}
